package com.yaozheng.vocationaltraining.service;

import android.content.Intent;
import android.net.Uri;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.Toast;

import com.yaozheng.vocationaltraining.BuildConfig;
import com.yaozheng.vocationaltraining.dialog.service.CancelAskDialog;
import com.yaozheng.vocationaltraining.dialog.service.ConfirmCancelAskDialog;
import com.yaozheng.vocationaltraining.iview.set_up.ICheckingVersionView;
import com.yaozheng.vocationaltraining.service.impl.set_up.CheckingVersionServiceImpl;
import com.yaozheng.vocationaltraining.service.set_up.CheckingVersionService;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.TypeUtils;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EService;
import org.androidannotations.annotations.UiThread;

/**
 * 版本服务  小米手机默认弹不出对话框
  */
@EService
public class VersionService extends BaseService implements ICheckingVersionView {
    private boolean isDestroy;
    String appverUrl;
    @Bean(CheckingVersionServiceImpl.class)
    CheckingVersionService checkingVersionService;

    ConfirmCancelAskDialog confirmCancelAskDialog;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        isDestroy = false;
        checkingVersionService.init(this);
//        Toast.makeText(this, "调试1", Toast.LENGTH_LONG).show();
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        checkingVersionService.checkingVersion();
//        Toast.makeText(this, "调试2", Toast.LENGTH_LONG).show();
        return START_NOT_STICKY;// START_REDELIVER_INTENT;
    }


    /**
     * 取消
     */
    public void cancelClick() {
//        Toast.makeText(this, "点击取消", Toast.LENGTH_LONG).show();
//		LogUtils.println("StratrVersion" + "confirm");
        //开始下载文件
        Intent exitIntent = new Intent();
        exitIntent.setAction(Constants.EXIT_ACTIVITYS_ACTION);
        sendBroadcast(exitIntent);


    }

    /**
     * 强制更新确定点击
     */
    public void determineClick() {
//		LogUtils.println("StratrVersion" + "cance");
        //开始下载文件
//        Toast.makeText(this, "点击确定"+appverUrl, Toast.LENGTH_LONG).show();
        Intent exitIntent = new Intent();
        exitIntent.setAction(Constants.EXIT_ACTIVITYS_ACTION);
        sendBroadcast(exitIntent);

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(appverUrl));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);

    }

    @Override
    public boolean isResponseResult() {
        return !isDestroy;
    }

    @UiThread
    @Override
    public void tokenFail() {
        //  Toast.makeText(this,"登录失效了" , Toast.LENGTH_LONG).show();
    }

    @Override
    public void onDestroy() {
        isDestroy = true;
//        Toast.makeText(this, "调试3", Toast.LENGTH_LONG).show();
        if (confirmCancelAskDialog != null && confirmCancelAskDialog.isShowing()) {
            confirmCancelAskDialog.cancel();
        }
        super.onDestroy();
    }

    @UiThread
    @Override
    public void checkingVersionSuccess(JSONObject jsonObject) {
//        BuildConfig.VERSION_NAME
//        Toast.makeText(this, BuildConfig.VERSION_NAME+"要更新"+jsonObject, Toast.LENGTH_LONG).show();
        JSONObject dataJsonObject = TypeUtils.getJsonObject(jsonObject, "data");
        String version = TypeUtils.getJsonString(dataJsonObject, "version");
        appverUrl = TypeUtils.getJsonString(dataJsonObject, "package");
        if (!BuildConfig.VERSION_NAME.equals(version)) {
            //更新
            if (confirmCancelAskDialog == null) {
                confirmCancelAskDialog = new ConfirmCancelAskDialog(this);
            }
            confirmCancelAskDialog.show();
            confirmCancelAskDialog.setTitleAndroidContent("版本更新", "宝沃学院有新版本，是否安装？");
        }

    }

    @UiThread
    @Override
    public void checkingVersionError(String error) {
        // Toast.makeText(this,"错误（"+error+"）" , Toast.LENGTH_LONG).show();
    }


}
